home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / DOCUMENTTEMPLATE.PY < prev    next >
Encoding:
Text File  |  1999-03-09  |  8.4 KB  |  205 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. '''Document templates with fill-in fields
  65.  
  66. Document templates provide for creation of textual documents, such as
  67. HTML pages, from template source by inserting data from python objects
  68. and name-spaces.
  69.  
  70. When a document template is created, a collection of default values to
  71. be inserted may be specified with a mapping object and with keyword
  72. arguments.
  73.  
  74. A document templated may be called to create a document with values
  75. inserted.  When called, an instance, a mapping object, and keyword
  76. arguments may be specified to provide values to be inserted.  If an
  77. instance is provided, the document template will try to look up values
  78. in the instance using getattr, so inheritence of values is supported.
  79. If an inserted value is a function, method, or class, then an attempt
  80. will be made to call the object to obtain values.  This allows
  81. instance methods to be included in documents.
  82.  
  83. Document templates masquerade as functions, so the python object
  84. publisher (Bobo) will call templates that are stored as instances of
  85. published objects. Bobo will pass the object the template was found in
  86. and the HTTP request object.
  87.  
  88. Two source formats are supported:
  89.  
  90.    Extended Python format strings (EPFS) --
  91.       This format is based on the insertion by name format strings
  92.       of python with additional format characters, '[' and ']' to
  93.       indicate block boundaries.  In addition, parameters may be
  94.       used within formats to control how insertion is done.
  95.  
  96.       For example:
  97.  
  98.          %%(date fmt=DayOfWeek upper)s
  99.  
  100.       causes the contents of variable 'date' to be inserted using
  101.       custom format 'DayOfWeek' and with all lower case letters
  102.       converted to upper case.
  103.  
  104.    HTML --
  105.       This format uses HTML server-side-include syntax with
  106.       commands for inserting text. Parameters may be included to
  107.       customize the operation of a command.
  108.  
  109.       For example:
  110.  
  111.          <!--#var total fmt=12.2f-->
  112.  
  113.       is used to insert the variable 'total' with the C format
  114.       '12.2f'.        
  115.  
  116. Document templates support conditional and sequence insertion
  117.  
  118.     Document templates extend python string substitition rules with a
  119.     mechanism that allows conditional insertion of template text and that
  120.     allows sequences to be inserted with element-wise insertion of
  121.     template text.
  122.  
  123. Access Control
  124.  
  125.     Document templates provide a basic level of access control by
  126.     preventing access to names beginning with an underscore.
  127.     Addational control may be provided by providing document templates
  128.     with a 'validate' method.  This would typically be done by
  129.     subclassing one or more of the DocumentTemplate classes.
  130.  
  131.     If provided, the the 'validate' method will be called when objects
  132.     are accessed as accessed as instance attributes or when they are
  133.     accessed through keyed access in an expression..  The 'validate'
  134.     method will be called with five arguments:
  135.  
  136.     1. The containing object that the object was accessed from,
  137.  
  138.     2. The actual containing object that the object was found in,
  139.        which may be different from the containing onject the object
  140.        was accessed from, if the containing object supports
  141.        acquisition,
  142.  
  143.     3. The name used to acces the object,
  144.  
  145.     4. The object, and
  146.  
  147.     5. The namespace object used to render the document template.
  148.  
  149.        If a document template was called from Bobo, then the namespace
  150.        object will have an attribute, AUTHENTICATED_USER that is the
  151.        user object that was found if and when Bobo authenticated a user.
  152.  
  153. Document Templates may be created 4 ways:
  154.  
  155.     DocumentTemplate.String -- Creates a document templated from a
  156.         string using an extended form of python string formatting.
  157.  
  158.     DocumentTemplate.File -- Creates a document templated bound to a
  159.         named file using an extended form of python string formatting.
  160.         If the object is pickled, the file name, rather than the file
  161.         contents is pickled.  When the object is unpickled, then the
  162.         file will be re-read to obtain the string.  Note that the file
  163.         will not be read until the document template is used the first
  164.         time.
  165.  
  166.     DocumentTemplate.HTML -- Creates a document templated from a
  167.         string using HTML server-side-include rather than
  168.         python-format-string syntax.
  169.  
  170.     DocumentTemplate.HTMLFile -- Creates an HTML document template
  171.         from a named file.
  172.  
  173. '''
  174.  
  175.  
  176. __version__='$Revision: 1.10 $'[11:-2]
  177.  
  178. ParseError='Document Template Parse Error'
  179.  
  180. from DT_String import String, File
  181. from DT_HTML import HTML, HTMLFile, HTMLDefault
  182. # import DT_UI # Install HTML editing
  183. from DT_Util import html_quote
  184.